﻿$(function() {
	/**
	 * 设备开动情况
	 * */
    var dayline = echarts.init(document.getElementById('singlechart'));
    optionline = {
            tooltip : {
                trigger: 'axis',
                axisPointer: {
    		        type: 'shadow'
    		    }
            },
            backgroundColor:'#e0e0e0',
            grid: {
                top: '15%',
                left: '4%',
                right: '5%',
                bottom:'20%',
                containLabel: true
            },
            legend: {
            	top: '10',
    	        data:['平均开动率','平均满载率']
    	    },
            xAxis:  {
                type: 'category',
                boundaryGap: [1, 1],
                data: [],//['1日','2日','3日','4日','5日','6日'],
                name:'小时',
                splitLine:{
                	show:false
                }
            },
            yAxis: {
            	min:0,
            	max:100,
                type: 'value',
                name:'开动率(%)',
                axisLabel:{
    	    		 formatter: '{value}'
    	    	 },
                splitLine:{
                	show:false
                }
            },
            dataZoom:{
    	    	type:'slider',
    	    	start:0,
    	    	end:100,
//    	    	labelFormatter: function (value) {
//    	    	    return value; // 返回最终的label内容。
//    	    	},
    	    	bottom:20
    	    },
            series: [{
                 name:'平均开动率',
                 type:'line',
                 data:[]//[0.8,0.9,0.84,0.7,0.8,0.91],
             },{
                 name:'平均满载率',
                 type:'line',
                 data:[]//[0.83,0.75,0.89,0.91,0.79,0.93],
             }]
        };
    dayline.setOption(optionline);
    
    
    var lineId=0;var equId=0;
    var	startTime="";
    var	endTime="";
    
    /**
     * slider滑动事件
     * */
    $('#ss').slider({
    	range: true,
        onComplete:function(value){//滑块值改变时触发
//	       	time=moment(value).format('YYYY-MM-DD');
	       	startTime=moment(value[0]).format('YYYY-MM-DD');
	       	endTime=moment(value[1]).format('YYYY-MM-DD');
	       	getData();
       }
    });
    /**
     * 初始化
     * */
    var vm = new Vue({
	    el: '#itemData',
	    data:{item:[]}
	});
    var vm2 = new Vue({
	    el: '#itemData2',
	    data:{item:[]}
	}); 

    var timess=[parseInt(moment().subtract(7, 'days').format("x")),parseInt(moment().format("x"))];
    $('#ss').slider('setValues',timess);
    var arr=$('#ss').slider('getValues');
    var time1=arr[0];
    var time2=arr[1];
    startTime=moment(time1).format("YYYY-MM-DD");
    endTime=moment(time2).format("YYYY-MM-DD");
    getData();
    function getData(){
    	console.log("startrate---complex");
    	$.post(basePath+"/line/StartrateComplex.json",{"lineId":lineId,"equId":equId,startDate:startTime,endDate:endTime},function(res){
    		vm.$set('item',res);
    		vm2.$set('item',res);
//    		if(res.success){
    			dayline.setOption({
			        xAxis: {
			            data: res.startRateDays
			        },
			        series: [{
			        	name:'平均开动率',
			            data: res.startRate
			        },{
			        	name:'平均满载率',
			            data: res.fullRate
			        }]
    			});
//    		}else{
//    			alert("日期无效");
//    		}	
    	});
    }
    
    function switchEquipment(){
    	$.post(basePath+"/line/switchEquipment/complex.json",{"equId":equId,startDate:startTime,endDate:endTime},function(res){
    		vm2.$set('item',res);
    		dayline.setOption({
		        xAxis: {
		            data: res.startRateDays
		        },
		        series: [{
		        	name:'平均开动率',
		            data: res.startRate
		        },{
		        	name:'平均满载率',
		            data: res.fullRate
		        }]
			});
    	});
    }
    
    /**
     * 自定义时间按钮的点击事件
     * */
	 $("#defineTime").click(function (){
		 var mePanel = $('#defineTimeWin');
	   	 mePanel.dialog('open');
	 });
	 
	 /**
	  * 切换生产线的点击事件
	  * */
	var mePanel = $('#switchEquip');
	$("#linetree").tree({
		onDblClick: function(node) {
			if (node.url) {
//				if(node.shifts){
					lineId=node.id;
					var arr=$('#ss').slider('getValues');
		 		    var time1=arr[0];
		 		    var time2=arr[1];
		 		    startTime=moment(time1).format("YYYY-MM-DD");
		 		    endTime=moment(time2).format("YYYY-MM-DD");
		 		    getData();
		 		    mePanel.dialog('close');
//				}else{
//					$.messager.alert({showSpeed:100,title:'消息提示',msg:'该生产线无班次，请重新选择',timeout:3000});
//				}
			}else{
				$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
			}
		}
	});
   $("#switchEquipOK").click(function(){
   	var row = $("#linetree").tree('getSelected');
   	console.log(row);
   	if(row&&row.url!=null){
		lineId=row.id;
		var arr=$('#ss').slider('getValues');
	    var time1=arr[0];
	    var time2=arr[1];
	    startTime=moment(time1).format("YYYY-MM-DD");
	    endTime=moment(time2).format("YYYY-MM-DD");
	    getData();
	    $('#switchEquip').dialog('close');
   	}else{
		$.messager.alert({showSpeed:100,title:'消息提示',msg:'请选择生产线！',timeout:3000});
	}
   });
   
   
   /**
    * 选择设备
    */
   $("#switchEquipbtn").click(function(){
		var equPanel = $('#allEquipsByLine');
		equPanel.dialog('open');
		$('#equip_data').datagrid({    
		    url: basePath+"/equip/findByLine.json",
		    queryParams:{lineId:lineId}
		});
   });
   $('#equip_data').datagrid({
		//双击设备时，根据选中设备刷新
		onDblClickRow: function(rowIndex, rowData){
			equId=rowData.id;
			switchEquipment();
		   $("#allEquipsByLine").dialog('close');
		}
	});
   $("#switchEquipByLineOK").click(function(){
	   	var equWin=$("#allEquipsByLine");
	   	var row=$('#equip_data').datagrid('getSelected');
	   	equId=row.id;
	   	switchEquipment();
	   	equWin.dialog('close');
   });
});